package com.guchenbo.study.aop;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class TxServiceAImpl implements TxServiceA {
    @Resource
    private TxServiceB txServiceB;

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void defaultTx(boolean a,boolean b) {
        try {
            txServiceB.defaultTx(b);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (a) {
            throw new RuntimeException("random error");
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void newTx(boolean a, boolean b) {
        try {
            txServiceB.newTx(b);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (a) {
            throw new RuntimeException("random error");
        }
    }
    @Override
    @Transactional(propagation = Propagation.NESTED)
    public void nestTx(boolean a, boolean b) {
        try {
            txServiceB.nestTx(b);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (a) {
            throw new RuntimeException("random error");
        }
    }
}
